package com.xiaoma.tpo.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.xiaoma.tpo.service.UserService;
import com.xiaoma.tpo.utils.Encrypt;

@Controller
@RequestMapping("/sys/user")
public class SysController {
    
    public static final String CURRENT_USER = "CURRENT_USER";
    
    @Autowired
    private UserService userService;
    
    /**
     * 上面的方法用的是sys_user表
     * 下面用的是t_user表
     */
    @RequestMapping(value = "/loginB", method = RequestMethod.POST)
    public String loginBg(@RequestParam(value = "user_name", required = true) String userName, @RequestParam(value = "user_pwd", required = true) String userPwd, HttpServletRequest request) {
        boolean flag = userService.loginSysNew(userName, userPwd);
        String location = "redirect:/sys/user/main";
        if (flag) {
            request.getSession().setAttribute("username", userName);
        } else {
        	location = "login";
        }
        return location;

    }
    @RequestMapping(value = "/loginB",method = RequestMethod.GET)
    public String loginB(){
        return "login";
        
    }
    @RequestMapping(value = "/logoutB",method = RequestMethod.GET)
    public String logoutB(HttpServletRequest request){
    	request.getSession().removeAttribute("username");
        return "redirect:/sys/user/login";
        
    }
    
    @RequestMapping(value = "/login",method = RequestMethod.GET)
    public String loginBg(){
        return "login";
        
    }
    
    @RequestMapping(value="/main", method = RequestMethod.GET)
    public String main(HttpSession session, Model model,HttpServletRequest request) {
    	String username = (String) request.getSession().getAttribute("username");
    	if(username == null || username ==""){
    		return "login";
    	}
        return "main";
    }
    
    @RequestMapping(value = "/logout",method = RequestMethod.GET)
    public String logout(HttpSession session){
        session.removeAttribute(CURRENT_USER);
        return "redirect:/sys/user/login";
        
    }
    
   
}
